import express from 'express'
export const router = express.Router()

import { createConnectionPool } from '../../mysql/index.js'

const connection = await createConnectionPool()

// 商户用户列表
router.post('/user/token_store', async (req, res) => {
    const { id, code, message, token, role, nickname } = req.body;

    // 校验请求参数
    if (!id || !code || !message || !token || !role || !nickname) {
        return res.status(400).send('All fields are required');
    }

    try {
        // 更新 token_store 表中的记录
        const query = `
      UPDATE token_store
      SET code = ?, message = ?, token = ?, role = ?, nickname = ?
      WHERE id = ?
    `;

        const [result] = await connection.execute(query, [code, message, token, role, nickname, id]);

        // 检查是否有记录被更新
        if (result.affectedRows === 0) {
            return res.status(404).send('Record with the given ID not found');
        }

        res.send('Token store updated successfully');
    } catch (error) {
        console.error(error);
        res.status(500).send('Internal Server Error');
    }
});


export default router
